'use strict';

module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/video/pub/all_series 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		

		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			user_id,
			tv_id,
			series,
			plus_type,
			invite_code,
			tfid
		} = data;
		let res = {
			code: 0,
			jump_serie: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------

		//前端传来集数，则原样返回.没传集数就从最近历史记录里查找
		if (series * 1 > 0) {
			res.jump_serie = series * 1
		} else {
			res.jump_serie = 0
			const history = await db.collection('a_history').where({
				user_id: user_id,
				tv_id: tv_id
			}).get()
			console.log("history=", history);

			if (history.data.length > 0 && history.data[0].series) {
				res.jump_serie = history.data[0].series * 1
			}
		}

		//获取前后5集的数据
		const tv_series_temp = await db.collection('a_tv_series').where({
			tv_id: data.tv_id,
			series: _.and(_.gte(res.jump_serie - 5), _.lte(res.jump_serie + 5))
		}).orderBy('series', 'asc').get()
		console.log("tv_series_temp=", tv_series_temp);
		try {
			//获取投放链接设置的卡点
			const agentset = await db.collection('a_agent_settingTV').where({
				tfid: tfid || "aa123321",
				tv_id: tv_id
			}).get()
			// console.log("90agentset=======================", agentset.data[0]);
			//赋值前后5集的剧集数据
			if (agentset.data.length != 0) {
				let agent_pay = agentset.data[0].agent_pay
				let agent_series = agentset.data[0].agent_series
				for (let i = 0; i < tv_series_temp.data.length; i++) {
					if (tv_series_temp.data[i].series >= agent_series) {
						tv_series_temp.data[i].pay = agent_pay
					} else if (agent_series >= tv_series_temp.data[i].series) {
						tv_series_temp.data[i].pay = 0
					}

				}
			}
		} catch (err) {

		}
		//赋值前后5集的剧集数据
		res.data = tv_series_temp.data
		

		//前后5集的已支付数据
		const paydata_temp = await db.collection('a_had_pay').where({
			tv_id: data.tv_id,
			user_id: data.user_id,
			series: _.and(_.gte(res.jump_serie - 5), _.lte(res.jump_serie + 5))
		}).orderBy('series', 'asc').get()

		//赋值前后5集的支付数据
		res.paydata = paydata_temp.data

		let tv_series = res.data
		//判断是否存在已支付数据
		if (res.paydata.length > 0) {
			var paydata = res.paydata
		} else {
			var paydata = []
		}

		console.log("tv_series.length=", res.data.length);

		for (var i = 0; i < tv_series.length; i++) {
			tv_series[i].had_pay = false;
			tv_series[i].like = false;
			tv_series[i].isplay = false;
			tv_series[i].playIng = false;
			tv_series[i].state = "pause";
			//paydata是从已支付数据库里查到的当前用户和当前剧的所有支付记录
			//一一匹配后，如果匹配成功，就把had_pay设为true
			if (plus_type == 1) {
				tv_series[i].had_pay = true
			} else {
				for (var j = 0; j < paydata.length; j++) {
					if (tv_series[i].series == paydata[j].series) {
						console.log("匹配成功");
						tv_series[i].had_pay = true
					}
				}
			}
		}

		// 业务逻辑结束-----------------------------------------------------------
		res.data = []
		res.paydata = []
		res.series = tv_series
		return res;

		
	}
}